package org.orz.cloud.core.config;

import org.orz.cloud.common.exception.BusinessException;
import org.orz.cloud.common.exception.SystemErrorCode;
import org.orz.cloud.common.result.ResultDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class ControllerAdvice {
    private static final Logger LOGGER = LoggerFactory.getLogger(ControllerAdvice.class);

    @ExceptionHandler(BusinessException.class)
    @ResponseStatus(HttpStatus.OK)
    public ResultDTO businessException(BusinessException e){
        LOGGER.error("业务异常...",e);
        LOGGER.info("==================================request end=========================================");
        return ResultDTO.fail(e.getErrorCode(), e.getErrorMessage(), null);
    }

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.OK)
    public ResultDTO exception(Exception e){
        LOGGER.error("服务异常...",e);
        LOGGER.info("==================================request end=========================================");
        return ResultDTO.fail(SystemErrorCode.SERVER_ERROR);
    }

}
